package com.sjm.suchbear.minibookmall;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sjm.suchbear.minibookmall.Utils.SP;
import com.sjm.suchbear.minibookmall.Utils.T;
import com.sjm.suchbear.minibookmall.View.MyListView;
import com.sjm.suchbear.minibookmall.action.CarManage;
import com.sjm.suchbear.minibookmall.action.OrderDetailManage;
import com.sjm.suchbear.minibookmall.action.OrderManage;
import com.sjm.suchbear.minibookmall.action.UserManage;
import com.sjm.suchbear.minibookmall.fragment.adapter.CarListAdapter;
import com.sjm.suchbear.minibookmall.model.CarModel;
import com.sjm.suchbear.minibookmall.model.OrderState;
import com.sjm.suchbear.minibookmall.model.UserModel;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderActivity extends Activity {

    @BindView(R.id.ll_header_back)
    LinearLayout llHeaderBack;
    @BindView(R.id.tv_list_title)
    TextView tvListTitle;
    @BindView(R.id.iv_header_function_ico)
    ImageView ivHeaderFunctionIco;
    @BindView(R.id.ll_header_function)
    LinearLayout llHeaderFunction;
    @BindView(R.id.ll_List_Main)
    RelativeLayout llListMain;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.lv_orderShow)
    MyListView lvOrderShow;
    @BindView(R.id.tv_button)
    TextView tvButton;
    @BindView(R.id.rl_button)
    RelativeLayout rlButton;
    @BindView(R.id.tv_totlePrice)
    TextView tvTotlePrice;

    private SQLiteDatabase dbread;
    private static List<CarModel> cars;
    private CarListAdapter carListAdapter;
    private UserModel userModel;
    private BigDecimal allTotal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        ButterKnife.bind(this);
        tvListTitle.setText("订单结算");
        BookDB db = new BookDB(getBaseContext());
        dbread = db.getReadableDatabase();
    }

    @Override
    protected void onResume() {
        super.onResume();
        cars = CarManage.getCarbyid(dbread, SP.getUid(getBaseContext()));
        if (cars == null) {
            T.showShort(getBaseContext(), "购物车数据获取失败");
            finish();
        }
        carListAdapter = new CarListAdapter(getBaseContext(), cars);
        lvOrderShow.setAdapter(carListAdapter);
        userModel = UserManage.findUserByID(dbread, SP.getUid(getBaseContext()));
        tvName.setText("收货人:      ".concat(userModel.getRealname()));
        tvPhone.setText("手机号:      ".concat(userModel.getPhone()));
        tvAddress.setText("收货地址:   ".concat(userModel.getAddress()));
        allTotal = new BigDecimal(0);
        for (int i = 0; i < cars.size(); i++) {
            BigDecimal cost = new BigDecimal(cars.get(i).getPrice());
            BigDecimal num = new BigDecimal(cars.get(i).getNum());
            BigDecimal total = cost.multiply(num);
            allTotal = allTotal.add(total);
        }
        DecimalFormat myformat = new java.text.DecimalFormat("0.00");
        tvTotlePrice.setText("总价：￥".concat(myformat.format(allTotal)));
    }

    @OnClick({R.id.ll_header_back, R.id.tv_button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_header_back:
                finish();
                break;
            case R.id.tv_button:
                String orderID = OrderManage.addOrder(
                        dbread,
                        SP.getUid(getBaseContext()),
                        OrderState.WAITPAY,
                        String.valueOf(allTotal),
                        userModel.getRealname(),
                        userModel.getPhone(),
                        userModel.getAddress());
                if (orderID == null) {
                    T.showShort(getBaseContext(), "结算失败，请重试");
                } else {
                    for (int i = 0; i < cars.size(); i++) {
                        BigDecimal cost = new BigDecimal(cars.get(i).getPrice());
                        BigDecimal num = new BigDecimal(cars.get(i).getNum());
                        BigDecimal total = cost.multiply(num);
                        OrderDetailManage.addOrderDetailByid(dbread, orderID, cars.get(i).getBookID(), cars.get(i).getBookname(), cars.get(i).getNum(), String.valueOf(total));
                    }
                    T.showShort(getBaseContext(), "提交成功，请尽快支付");
                    Intent intent = new Intent(OrderActivity.this, OrderDetailActivity.class);
                    intent.putExtra("orderID", orderID);
                    startActivity(intent);
                    finish();
                }
                break;
        }
    }
}
